<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <FullDeductionDetailDrawer @register="registerFullDeductionDetailDrawer" @success="handleSuccess"></FullDeductionDetailDrawer>
    <FullDeductionDetailAddDrawer @register="registerFullDeductionDetailAddDrawer" @success="handleSuccess"></FullDeductionDetailAddDrawer>
  </div>
</template>
<script lang="ts" setup name="fullDeductionRecord">
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/table';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import { columns, searchFormSchema } from './data/data';
import { Api } from './data/api';
import FullDeductionDetailDrawer from './components/FullDeductionDetailDrawer.vue';
import FullDeductionDetailAddDrawer from '../fullDeductionDetail/components/AddDrawer.vue';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';

const { createConfirm } = useMessage();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'fullDeductionRecord-template',
  tableProps: {
    //表格标题
    title: '全额抵扣记录列表',
    //主键字段
    rowKey: 'fullDeductionId',
    //列表接口
    listUrl: Api.page,
    //列表查询handler
    handlerName: 'busFullDeductionRecordListHandler',
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      showExportButton: true,
    },
    showActionColumn: true,
    //操作列配置
    actionColumn: {
      width: 200,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    clickToRowSelect: true,
    rowSelection: {
      type: 'radio',
    },
  },
});

const [registerTable, { reload }, { rowSelection, hasSelectedRows }] = tableContext;

const [registerFullDeductionDetailDrawer, { openDrawer: openFullDeductionDetailDrawer }] = useDrawer();
const [registerFullDeductionDetailAddDrawer, { openDrawer: openFullDeductionDetailAddDrawer }] = useDrawer();

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '全额抵扣撤销(商城)',
      onClick: handlerDeductionCancel.bind(null, record),
      ifShow: record.isMallFullDeduction && record.fullDeductionState == 0,
      auth: 'ACTION_deductionCancelMall',
    },
    {
      label: '全额抵扣撤销(非商城)',
      onClick: handlerDeductionCancel.bind(null, record),
      ifShow: !record.isMallFullDeduction && record.fullDeductionState == 0,
      auth: 'ACTION_deductionCancelOther',
    },
  ];
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '查看抵扣详情',
      onClick: handlerLookFullDeduction.bind(null, record),
      auth: 'ACTION_lookDeductionDetail',
    },
    {
      label: '新增抵扣日志',
      onClick: handlerAddFullDeductionDetail.bind(null, record),
      auth: 'ACTION_addDeductionDetail',
    },
    {
      label: '退卡撤销(商城)',
      onClick: handlerTuiKanCancel.bind(null, record),
      ifShow: record.isMallFullDeduction && record.vipCardState == 5,
      auth: 'ACTION_deductionTuiKanCancelMall',
    },
    {
      label: '退卡撤销(非商城)',
      onClick: handlerTuiKanCancel.bind(null, record),
      ifShow: !record.isMallFullDeduction && record.vipCardState == 5,
      auth: 'ACTION_deductionTuiKanCancelOther',
    },
  ];
}

/**
 * 全额抵扣撤销
 */
function handlerDeductionCancel(record) {
  createConfirm({
    iconType: 'info',
    title: '确认全额抵扣撤销吗？',
    content: '请确认会员是否存在抵扣记录，谨慎操作，全额抵扣撤销会将本条抵扣记录和抵扣日志标记为撤销状态？',
    onOk: async () => {
      await handlePost(Api.cancel, { fullDeductionId: record.fullDeductionId });
      reload();
    },
  });
}

/**
 * 退卡撤销
 */
function handlerTuiKanCancel(record) {
  createConfirm({
    iconType: 'info',
    title: '确认退卡撤销吗？',
    content: '退卡撤销会变更会员卡状态为正常，不会撤销会员的抵扣记录和抵扣日志？',
    onOk: async () => {
      await handlePost(Api.tuiKaCancel, { fullDeductionId: record.fullDeductionId });
      reload();
    },
  });
}

/**
 * 查看抵扣详情
 */
function handlerLookFullDeduction(record) {
  openFullDeductionDetailDrawer(true, {
    record,
    isUpdate: true,
  });
}

/**
 * 新增抵扣日志
 */
function handlerAddFullDeductionDetail(record) {
  openFullDeductionDetailAddDrawer(true, {
    record,
    isUpdate: false,
  });
}
</script>
